package main

import (
	"os"
	"strings"

	"web-service-gin/src/app"

	"github.com/toolkits/pkg/errorx"
	"github.com/urfave/cli/v2"
)

func main() {
	newApp := cli.NewApp()
	newApp.Name = "newApp"
	newApp.Version = "v1"
	newApp.Usage = "Gin api service"
	newApp.Commands = []*cli.Command{
		newAppCmd(),
	}
	newApp.Run(os.Args)
}

func newAppCmd() *cli.Command {
	return &cli.Command{
		Name:  "service",
		Usage: "Run service",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "conf",
				Aliases: []string{"c"},
				Usage:   "specify configuration file(.json)",
			},
		},
		Action: func(c *cli.Context) error {
			conf := c.String("conf")
			if conf == "" {
				errorx.Bomb(1, "not found config file")
			}
			if !strings.HasSuffix(conf, ".yaml") {
				errorx.Bomb(1, "file format error")
			}
			app.Run(app.SetConfig(conf))
			return nil
		},
	}
}
